#   ============================================================================
#   Author: Youhua Xu
#	Date:	2021-12-01
#	
#   ============================================================================

#   Make a build dir for compilation
MDIR := $(shell pwd)
WRKDIR = $(MDIR)/build
BINDIR = $(MDIR)/bin

#	make build & binary dirs
.base:
	if ! [ -e $(WRKDIR) ]; then mkdir $(WRKDIR) ; fi;
	touch $(WRKDIR)/.base;
	if ! [ -e $(BINDIR) ]; then mkdir $(BINDIR) ; fi;
	touch $(BINDIR)/.base;
	# if ! [ -e $(DEBUG) ]; then mkdir $(DEBUG) ; fi;
	# touch $(DEBUG)/.base;
#   ============================================================================
#   Set the source file path
vpath %.c   main:src:tests
vpath %.h   include
vpath %.o 	build
vpath .base build

INCLUDES = -I $(MDIR)/include

GCC			= gcc
# CC			= arm-linux-gnueabihf-gcc
# CC			= gcc
CCFLAG  	= -Wall #-fcommon
OPTFLAG		= #-O2 #-ffast-math #( not recognized by intel compiler )
LIBS		= -lm -lpthread
LDFLAG      = 

# CCFLAG		+= -D_SPI_DEBUG_	# turn on SPI information
CCFLAG		+= -D_DEBUG_DACX0508_
# CCFLAG		+= -D_DEBUG_PS_SPI_
CCFLAG		+= -D_DEBUG_CCD_DRV_
# CCFLGA		+= -D_DEBUG_CCD_DRV_PARAMS_	# print drv parameters

CCFLAG      += -D_ENABLE_PINNING_BEFORE_EXPOSURE_	# 控制曝光开始前是否pinning
CCFLAG      += -D_ENABLE_REFRESH_BEFORE_EXPOSURE_   # 

# CCFLAG		+= -D_ENABLE_FASTER_READOUT_

CCFLAG		+= -D_DEBUG_SOCKET_SERVER_

CCFLAG		+= -D_SOCKET_USE_UDP_

#   http://www.tuicool.com/articles/NBfeYj
ifeq ($(shell uname -s),Linux)
	LDFLAG	+= -Wl,--no-as-needed
	
	ifeq ($(shell which arm-linux-gnueabihf-gcc),)
		CC	= gcc
	else
		CC	= arm-linux-gnueabihf-gcc
	endif
endif

ifeq ($(shell uname -s),Darwin)
	LDFLAG	+= -framework Accelerate #(-framework Accelerate is for Mac OSX)
	CC		= gcc
endif

%.o: %.c .base
	cd $(WRKDIR);$(CC) $(OMPFLAG) $(OPTFLAG) $(CCFLAG) $(INCLUDES) -c ../$< -o $*.o

# MAIN
MAIN_READOUT    		= CCD290_readout.o

MAIN_DDR_WR_CFG			= DDR_WR_CFG.o

MAIN_IDELAY_CFG			= IDELAY_CFG.o
MAIN_IDELAYCRT_RST  	= idelayctrl_rst.o

MAIN_BONN_SHUTTER		= MAIN_BONN_SHUTTER.o

MAIN_CCD290_EXPOSURE	= CCD290_exposure.o

MAIN_CCD290_PIN			= CCD290_pin.o
MAIN_CCD290_REFRESH		= CCD290_refresh.o

MAIN_OS_SELECTOR		= OS_Selector.o
MAIN_SAE_WAVEFORM		= save_WaveForm.o

MAIN_RD_DRIVE_CONFIG	= CCD290_drive_config.o

MAIN_DCDS_WEIGHTS_CONFIG= DCDS_WEIGHTS_Config.o


# MAIN_MEPHISTO_SOCKET_SERVER = mephisto_socket_server_multi_threads.o
# MAIN_MEPHISTO_SOCKET_SERVER = mephisto_socket_server_multi_threads_udp.o
MAIN_MEPHISTO_SOCKET_SERVER = mephisto_socket_server_udp.o
#MAIN_MEPHISTO_SOCKET_SERVER = mephisto_socket_server_udp_bak20241206.o

MAIN_PL_IS_MASTER       = PL_isMaster.o


# DEPS
DEPS	= iniparser.o dictionary.o misc.o common.o spi.o  \
		spi_ps.o spi_ps_new.o BONN_SHUTTER.o THORLABS_SHUTTER.o  \
		spi_float_ps.o CCD290_DRV_Config.o ADC2REG_CLK.o ReadOut.o \
		MephistoCAM_Config.o float2bin.o

all:  ccd290_readout ddr_wr_en \
    cfg_idelay rst_idelayctrl shutter  ccd290_exposure PL_isMaster \
     ccd290_drive_config dcds_w_cfg mephisto_socket_server 

#     sread ccd290_pin ccd290_refresh run_socket_server_44s run_socket_server_scan_44s \
# 	run_socket_server_ad23high run_socket_server ccd290_readout_ad23high run_socket_server_scan


ccd290_readout: ${MAIN_READOUT} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ddr_wr_en: ${MAIN_DDR_WR_CFG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

rst_idelayctrl: ${MAIN_IDELAYCRT_RST} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

cfg_idelay: ${MAIN_IDELAY_CFG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

shutter: ${MAIN_BONN_SHUTTER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
thorlabs_shutter: ${MAIN_THORLABS_SHUTTER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_exposure: ${MAIN_CCD290_EXPOSURE} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_drive_config: ${MAIN_RD_DRIVE_CONFIG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

dcds_w_cfg: ${MAIN_DCDS_WEIGHTS_CONFIG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@	

mephisto_socket_server: ${MAIN_MEPHISTO_SOCKET_SERVER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

PL_isMaster: ${MAIN_PL_IS_MASTER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@	

####################################

dcds:
	cd dcds_extractor; make; cd --;

.PHONY:clean tidy run
clean: .base
	rm -rf $(WRKDIR);
tidy:
	make clean; rm -rf $(BINDIR) $(WRKDIR)
